package com.leen.admin.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.leen.admin.constant.AdminConstant;
import com.leen.admin.dto.DictDetailDTO;
import com.leen.admin.entity.DictDetail;
import com.leen.admin.repository.DictdetailRepository;
import com.leen.admin.service.DictdetailService;
import com.leen.admin.vo.DictDetailVO;
import com.leen.common.vo.ResponseVO;
import com.leen.common.vo.ResultEnum;
import com.leen.common.vo.ResultVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Transactional(rollbackFor = Exception.class)
@Service
public class DictdetailServiceImpl extends CurdServiceImpl<DictDetail,Long> implements DictdetailService {
    private DictdetailRepository dictdetailRepository;

    @Autowired
    private StringRedisTemplate  stringRedisTemplate;



    @Autowired
    public void setDictdetailRepository(DictdetailRepository dictdetailRepository){
        this.dictdetailRepository=dictdetailRepository;
        //给父类共值
        super.setCrudRepository(dictdetailRepository);
    }

    @Override
    public ResponseVO search(DictDetailDTO dictDetailDTO) {
        //分页页码
        Pageable pageable = PageRequest.of(dictDetailDTO.getPageNumber() - 1, dictDetailDTO.getPageSize());
        //构建查询对象
        Specification<DictDetail> specification = new Specification<DictDetail>() {
            @Override
            public Predicate toPredicate(Root<DictDetail> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> arr = new ArrayList<>();
                arr.add(criteriaBuilder.equal(root.get("value"), dictDetailDTO.getValue()));
                if (StringUtils.isNotBlank(dictDetailDTO.getLabel())) {
                    arr.add(criteriaBuilder.like(root.get("label"), "%" + dictDetailDTO.getLabel() + "%"));
                }

                return criteriaBuilder.and(arr.toArray(new Predicate[arr.size()]));
            }
        };
        //查询
        Page<DictDetail> page = this.dictdetailRepository.findAll(specification,pageable);
        return new ResponseVO(page.getTotalElements(), page.getContent());
    }

    @Override
    public List<DictDetailVO> findByDictId(long dictId) {
        if(dictId<=0){
            return  null;
        }

        List<DictDetailVO>  arrList=new ArrayList<>();

        String redisKey=AdminConstant.DICT_DETAILS_REDIS_KEY+dictId;
        //制作缓存
        String redisValue=this.stringRedisTemplate.opsForValue().get(AdminConstant.DICT_DETAILS_REDIS_KEY);

        if(StringUtils.isNotBlank(redisValue)){
            //转换成自定义对象
            arrList= JSONArray.parseArray(redisValue,DictDetailVO.class);
        }else{
            List<DictDetail> arrDetails = this.dictdetailRepository.findByDictId(dictId);
            //组装值
            for(DictDetail  detail : arrDetails){
                DictDetailVO  detailVO=new DictDetailVO();
                detailVO.setDictLabel( detail.getLabel());
                detailVO.setDictValue( detail.getValue());
                arrList.add(detailVO);
            }
            //放到redis中
            redisValue= JSONArray.toJSONString(arrList);
            this.stringRedisTemplate.opsForValue().set( redisKey, redisValue,AdminConstant.DICT_DETAILS_REDIS_KEY_EXPIRED_DAYS, TimeUnit.DAYS);
        }

        return arrList;
    }


}
